package com.chookss.home.audit.clockIn;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.base.StaticClass;
import com.chookss.home.entity.AskEntity;
import com.chookss.tools.ClearMoreZeroUtil;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.OkGoUtil;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.google.gson.reflect.TypeToken;
import com.johnrambo.ktea.ktExtends.ShareUtils;
import com.lvgroup.hospital.base.BaseAct;

import org.jetbrains.annotations.Nullable;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//考勤确认
public class ClockInSureActivity extends BaseAct {

    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.tvNormal)
    TextView tvNormal;
    @BindView(R.id.tvNormal1)
    TextView tvNormal1;
    @BindView(R.id.tvNormal2)
    TextView tvNormal2;
    @BindView(R.id.tvNormal3)
    TextView tvNormal3;
    @BindView(R.id.tvLate)
    TextView tvLate;
    @BindView(R.id.tvEarly)
    TextView tvEarly;
    @BindView(R.id.tvAbsent)
    TextView tvAbsent;
    @BindView(R.id.tvAsk)
    TextView tvAsk;
    @BindView(R.id.rvView)
    RecyclerView rvView;
    @BindView(R.id.rlBottom)
    RelativeLayout rlBottom;
    private AskAdapter askAdapter;
    private List<AskEntity> askEntityList = new ArrayList<>();
    private String time = "",noticeCode="";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clockin_sure);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        time = getIntent().getStringExtra("time");
        noticeCode = getIntent().getStringExtra("noticeCode");
        if (Utils.isNull(time)) {
            time = Utils.getCurrentTime2().substring(0, 7);
        }else{
            if(time.length()>6){
                time =time.substring(0, 7);
            }
        }
        commonTitleTxt.setText(time.substring(5,7)+"月考勤确认");
        GridLayoutManager gridLayoutManager1 = new GridLayoutManager(this, 3);
        rvView.setLayoutManager(gridLayoutManager1);
//        GridSpacingItemDecoration gridSpacingItemDecoration = new GridSpacingItemDecoration(3, Utils.dip2px(this, 12), false);
//        rvView.addItemDecoration(gridSpacingItemDecoration);
        askAdapter = new AskAdapter(R.layout.item_clockin_sure, this, askEntityList);
        rvView.setAdapter(askAdapter);
        getMessageNotice();
        selectDetail();
        selectList();
    }
    private void selectDetail() {
        //审批基本数据
        Map<String, String> map = new HashMap<>();
        map.put("month", time);
        map.put("employeeCode", new ShareUtils().getString(this, StaticClass.EMPLOYEECODE,""));
        showLoading();
        MyHttpRequest.getDefault().getRequestCompat(null, Urls.selectEmployeeDakaByMonth, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) {
                try {
                    dismissLoading();
                    JSONObject jsonObject = new JSONObject(data);
                    if ("200".equals(jsonObject.getString("code"))) {
                        JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                        if(jsonObject1.has("confirm")){
                            String confirm=jsonObject1.getString("confirm");
                            if(!Utils.isNull(confirm)){
                               if(Utils.changeNumber(confirm)==1){
                                   rlBottom.setVisibility(View.GONE);
                               }else{
                                   rlBottom.setVisibility(View.VISIBLE);
                               }
                            }else{
                                rlBottom.setVisibility(View.VISIBLE);
                            }
                        }else{
                            rlBottom.setVisibility(View.VISIBLE);
                        }
                        float  zhengchangNum=Float.parseFloat(jsonObject1.getString("zhengchangNum"));
                        float waiqinNum=Float.parseFloat(jsonObject1.getString("waiqinNum"));
                        float  bukaNum=Float.parseFloat(jsonObject1.getString("bukaNum"));
                        tvNormal.setText(ClearMoreZeroUtil.subZeroAndDot((zhengchangNum+waiqinNum+bukaNum)+"")+"天");
                        tvNormal1.setText("正常"+ClearMoreZeroUtil.subZeroAndDot(jsonObject1.getString("zhengchangNum"))+"天");
                        tvNormal2.setText("外勤"+ClearMoreZeroUtil.subZeroAndDot(jsonObject1.getString("waiqinNum"))+"天");
                        tvNormal3.setText("补卡"+ClearMoreZeroUtil.subZeroAndDot(jsonObject1.getString("bukaNum"))+"天");
                        tvLate.setText(ClearMoreZeroUtil.subZeroAndDot(jsonObject1.getString("chidaoNum"))+"天");
                        tvEarly.setText(ClearMoreZeroUtil.subZeroAndDot(jsonObject1.getString("zaotuiNum"))+"天");
                        tvAbsent.setText(ClearMoreZeroUtil.subZeroAndDot(jsonObject1.getString("kuangkongNum"))+"天");
                        tvAsk.setText(ClearMoreZeroUtil.subZeroAndDot(jsonObject1.getString("qingjiaNum"))+"天");
                    } else {
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"),jsonObject.getString("msg"));
                        }else{
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    MyToast.show("服务异常，请稍后重试");
                }
            }

            @Override
            public void onError(String errorMsg) {
                dismissLoading();
            }
        });
    }
    private void selectList() {
        //审批基本数据
        Map<String, String> map = new HashMap<>();
        map.put("month",time);
        map.put("employeeCode", new ShareUtils().getString(this, StaticClass.EMPLOYEECODE,""));
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<List<AskEntity>>>() {
        }.getType(), null, Urls.selectEmployeeQingjiaByMonth, SignUtils2.makeSignStr(map), false, new ObserverCallback<List<AskEntity>>() {
            @Override
            public void onSuccess(List<AskEntity> data) {

                askEntityList.clear();
                askEntityList.addAll(data);
                askAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(String errorMsg) {
                dismissLoading();
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }
    private void uploadData() {
        //审批基本数据
        Map<String, String> map = new HashMap<>();
        map.put("month",time);
        map.put("employeeCode", new ShareUtils().getString(this, StaticClass.EMPLOYEECODE,""));
        MyHttpRequest.getDefault().postRequestCompat(null, Urls.confirmMydaka, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) throws Exception {
                JSONObject reObject = new JSONObject(data);
                if ("200".equals(reObject.getString("code"))) {
                    MyToast.show("确认成功");
                    finish();
                } else {
                    MyToast.show(reObject.getString("msg"));
                }
            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }
    private void getMessageNotice() {
        //审批基本数据
        Map<String, String> map = new HashMap<>();
        map.put("noticeCode",noticeCode);
        MyHttpRequest.getDefault().postRequestCompat(null, Urls.messageNotice, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) throws Exception {
                JSONObject reObject = new JSONObject(data);
                if ("200".equals(reObject.getString("code"))) {

                }
            }
            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }
    class AskAdapter extends BaseQuickAdapter<AskEntity, BaseViewHolderEx> {
        private Activity context;

        public AskAdapter(int layoutResId, Activity context, @io.reactivex.annotations.Nullable List<AskEntity> data) {
            super(layoutResId, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolderEx helper, AskEntity entity) {
            LinearLayout llBg = helper.getView(R.id.llBg);
            TextView tvTitle = helper.getView(R.id.tvTitle);
            if(helper.getItemPosition()%3==1){
                llBg.setGravity(Gravity.CENTER);
            }else if(helper.getItemPosition()%3==2){
                llBg.setGravity(Gravity.RIGHT);
            }else{
                llBg.setGravity(Gravity.LEFT);
            }
            tvTitle.setText(entity.getBaoixao_type_name() +ClearMoreZeroUtil.subZeroAndDot(entity.getQingjiaNum()) + "天");
        }
    }
    @OnClick({R.id.common_title_back, R.id.tvSure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.tvSure:
                if (Urls.antiShake.check()) {
                    return;
                }
                uploadData();
                break;
        }
    }
}
